package com.test.javafaq_portal.controller;


import com.github.pagehelper.PageInfo;
import com.test.javafaq_portal.entity.Question;
import com.test.javafaq_portal.service.IQuestionService;
import com.test.javafaq_portal.service.ServiceException;
import com.test.javafaq_portal.vo.QuestionVo;
import com.test.javafaq_portal.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author test.com
 * @since 2025-04-11
 */
@RestController
@RequestMapping("/v1/questions")
@Slf4j
public class QuestionController {

    @Autowired
    IQuestionService questionService;

    @PostMapping
    public R createQuestion(@Validated QuestionVo questionVo, BindingResult res){
        if(res.hasErrors()){
            String msg = res.getFieldError().getDefaultMessage();
            return R.unproecsableEntity(msg);
        }
        if(questionVo.getTagNames().length == 0){
            return R.unproecsableEntity("至少选择一个标签");
        }
        if(questionVo.getTeacherNicknames().length == 0){
            return R.unproecsableEntity("至少选择一个老师");
        }
        //log.debug("{正在执行新增：{}",questionVo);
        try{
            questionService.saveQuestion(questionVo);
            return R.ok("提交成功!");
        }catch (ServiceException e){
            return R.failed(e);
        }

    }

    //查询当前登陆学生的所有问题
    @GetMapping("/my")
    public R<PageInfo<Question>> my(Integer pageNum){
        if (pageNum == null){
            pageNum = 1;
        }
        Integer pageSize = 8;
        log.debug("查询当前登陆学生的所有问题========");
        try{
            PageInfo<Question> list = questionService.getMyQuestions(pageNum, pageSize);
            return R.ok(list);
        }catch (ServiceException e){
            return R.failed(e);
        }

    }
    //查询当前登陆老师的所有问题
    //添加权限

    @GetMapping("/teacher")
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    public R<PageInfo<Question>> myTeacher(@AuthenticationPrincipal UserDetails user, Integer pageNum){
        if (pageNum == null){
            pageNum = 1;
        }
        Integer pageSize = 8;
        log.debug("查询当前登陆老师的所有问题========");
        try{
            PageInfo<Question> list = questionService.getTeacherQuestions(user.getUsername(),pageNum, pageSize);
            return R.ok(list);
        }catch (ServiceException e){
            return R.failed(e);
        }

    }
    //展示问题详情
    @GetMapping("/detail/{id}")
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    public R<Question> detail(@PathVariable("id") Integer id){
        if(id==null)
            return R.failed(new Throwable("id不能为空"));
        Question question = questionService.getQuestionById(id);
        return R.ok(question);
    }
}
